package com.medicine.controller;


import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.medicine.common.JsonMessageUtils;
import com.medicine.model.User;
import com.medicine.service.IUserService;


@Controller
public class LoginController {
	
	@Resource
	private IUserService userService;
	
	@RequestMapping(value="/login.html",method=RequestMethod.POST)
	@ResponseBody
	public String HelloWorld(@RequestParam(value="username") String username,@RequestParam(value="passwd")String passwd){
		System.out.println("username:"+username+",pwd:"+passwd);
		try {
			User model = new User();
			model.setUsername(username);
			model.setUserpwd(passwd);
			List<User> users = userService.selectByModel(model);
			String msg = null;
			if(null == users || users.size()==0){
				msg = "用户:"+username+"不存在或者错误";
			}else{
				model = users.get(0);
				model.setLastlogintime(new Timestamp(System.currentTimeMillis()));
				int isUpdate = userService.updateByPrimaryKey(model);//更新
				msg = "登录成功";
			}
			return JsonMessageUtils.getSuccessJson(msg, null);
		} catch (Exception e) {
			return JsonMessageUtils.getErrorJson("登录失败", e.getMessage());
		}
		
	}

}
